package org.jeecg.demo.wx.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;

import org.jeecg.demo.wx.dto.*;
import org.jeecg.demo.wx.entity.RlTask;
import org.jeecg.demo.wx.service.IRlTaskService;
import org.jeecg.demo.wx.util.PageData;
import org.jeecg.demo.wx.vo.*;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


@Api(tags="rl_task")
@RestController
@RequestMapping("/calendar/rlTask")
@Slf4j
public class RlTaskController extends JeecgController<RlTask, IRlTaskService> {
	@Autowired
	private IRlTaskService rlTaskService;

//	@ApiOperation(value="rl_task-分页列表查询", notes="rl_task-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<IPage<RlTask>> queryPageList(@RequestBody QueryTaskWebDto dto) {
//		return Result.OK(rlTaskService.queryPageInfo(dto, dto.getPageNo(), dto.getPageSize()));
//	}

	@AutoLog(value = "查看任务详情")
	@ApiOperation(value = "查看任务详情", notes = "查看任务详情")
	@PostMapping(value = "/queryTaskInfo")
	public Result<OneTaskVO> queryTaskInfo(@RequestParam(value = "id") Integer id) {
		OneTaskVO result = rlTaskService.queryTaskInfo(id);
		return Result.OK(result);
	}


	/**
	 * 	上传文件不能用requestbody
	 * 	小程序的上传逻辑是先上传文件再把返回的信息填入filename和path
	 * 	它的上传接口是jeecg自带的
	 * @param task
	 * @return
	 */
	@AutoLog(value = "用户派发任务")
	@ApiOperation(value = "用户派发任务", notes = "用户派发任务")
	@PostMapping(value = "/addTask")
	public Result<String> addTask(@RequestBody RlTaskSaveDto task) {
		rlTaskService.saveDispatchTask(task);
		return Result.OK("添加成功！");
	}

	@AutoLog(value = "用户派发任务Web端")
	@ApiOperation(value = "用户派发任务Web端", notes = "用户派发任务Web端")
	@PostMapping(value = "/addTaskFromWeb")
	public Result<String> addTaskFromWeb(RlTaskWebSaveDto task) {
		rlTaskService.saveDispatchTaskFromWeb(task);
		return Result.OK("添加成功！");
	}


	@AutoLog(value = "用户查看派发管理")
	@ApiOperation(value = "用户查看派发管理", notes = "用户查看派发管理")
	@PostMapping(value = "/queryDispatchTask")
	public Result<PageData<QueryTaskVO>> queryDispatchTask(@RequestBody QueryTaskDto dto) {
		PageData<QueryTaskVO> result = rlTaskService.queryDispatchTask(dto);
		return Result.OK(result);
	}

	@AutoLog(value = "用户修改派发任务")
	@ApiOperation(value = "用户修改派发任务", notes = "用户修改派发任务")
	@PostMapping(value = "/updateDispatchTask")
	public Result<String> updateDispatchTask(@RequestBody TaskUpdateDto dto) {
		rlTaskService.updateDispatchTask(dto);
		return Result.OK("编辑成功！");
	}


	@AutoLog(value = "用户修改派发任务Web端")
	@ApiOperation(value = "用户修改派发任务Web端", notes = "用户修改派发任务Web端")
	@PostMapping(value = "/updateDispatchTaskFromWeb")
	public Result<String> updateDispatchTaskFromWeb(RlTaskWebSaveDto task) {
		rlTaskService.updateDispatchTaskFromWeb(task);
		return Result.OK("编辑成功！");
	}


	@AutoLog(value = "用户查看快速处理")
	@ApiOperation(value = "用户查看快速处理", notes = "用户查看快速处理")
	@PostMapping("/queryUserTask")
	public Result<Page<QueryTaskVO>> queryUserTask(@RequestBody QueryTaskUserDto dto) {
		List<QueryTaskVO> result = rlTaskService.queryUserTask(dto);
		return Result.OK(new Page<QueryTaskVO>().setRecords(result));
	}

	@AutoLog(value = "用户快速处理")
	@ApiOperation(value = "用户快速处理", notes = "用户快速处理")
	@PostMapping("/modifyTaskState")
	public Result<String> modifyTaskState(@RequestBody ModifyStateDto dto) {
		rlTaskService.modifyTaskState(dto);
		return Result.OK("任务状态修改成功！");
	}

	@AutoLog(value = "查找用户")
	@ApiOperation(value = "查找用户", notes = "查找用户")
	@PostMapping("/queryAllUser")
	public Result<Page<SysUser>> queryAllUser(@RequestBody QueryUserDto dto) {
		List<SysUser> result = rlTaskService.queryAllUser(dto);
		return Result.OK(new Page<SysUser>().setRecords(result));
	}

	// 撤回
	@AutoLog(value = "撤回任务")
	@ApiOperation(value = "撤回任务", notes = "撤回任务")
	@PostMapping("/revokeUserTask")
	public Result<String> revokeUserTask(@RequestParam(name = "id", required = true) Integer id) {
		rlTaskService.revokeUserTask(id);
		return Result.OK("撤回任务成功！");
	}

	//轮询

	/**
	 * 统计任务状态
	 * @param id
	 * @return
	 */
	@AutoLog(value = "统计任务状态")
	@ApiOperation(value = "统计任务状态", notes = "统计任务状态")
	@PostMapping("/countTaskState")
	public Result<TaskCompletedVO> countTaskState(@RequestParam(name = "id", required = true) String id) {
		TaskCompletedVO result = rlTaskService.countTaskState(id);
		return Result.OK(result);
	}

	@AutoLog(value = "修复用户数据")
	@ApiOperation(value = "修复用户数据", notes = "修复用户数据")
	@PostMapping("/fixUserInfo")
	public Result<String> fixUserInfo() {
		rlTaskService.fixUserInfo();
		return Result.OK("操作成功");
	}

	@AutoLog(value = "获得部门用户分组")
	@ApiOperation(value = "获得部门用户分组", notes = "获得部门用户分组")
	@PostMapping("/queryUserByDepart")
	public Result<List<UserTreeVO>> queryUserByDepart() {
		List<UserTreeVO> result = rlTaskService.queryUserByDepart();
		return Result.OK(result);
	}

	@AutoLog(value = "获得个人派发任务信息详情，默认获取前30条信息")
	@ApiOperation(value = "获得个人派发任务信息详情，默认获取前30条信息，state取50表示派发", notes = "获得个人派发任务信息详情，默认获取前30条信息")
	@PostMapping("/queryUserTaskInfo")
	public Result<Page<RlTask>> queryUserTaskInfo(@RequestBody QueryUserTaskInfoDto dto) {
		Page<RlTask> result = rlTaskService.queryUserTaskInfo(dto);
		return Result.OK(result);
	}

	@AutoLog(value = "获得二级部门用户分组")
	@ApiOperation(value = "获得二级部门用户分组", notes = "获得二级部门用户分组")
	@PostMapping("/queryUserByDeparts")
	public Result<List<UserTreeSecondVO>> queryUserByDeparts() {
		List<UserTreeSecondVO> result = rlTaskService.queryUserByDeparts();
		return Result.OK(result);
	}


	 /*------------------------------------------------------------------------------------*/
	 //以下为生成低代码
	 /*------------------------------------------------------------------------------------*/
	/**
	 * 分页列表查询
	 *
	 * @param rlTask
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "rl_task-分页列表查询")
	@ApiOperation(value="rl_task-分页列表查询", notes="rl_task-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RlTask>> queryPageList(RlTask rlTask,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
//		QueryWrapper<RlTask> queryWrapper = QueryGenerator.initQueryWrapper(rlTask, req.getParameterMap());
		QueryWrapper<RlTask> queryWrapper = new QueryWrapper<>();
		String beginTime = req.getParameter("beginTime");
		String endTime = req.getParameter("endTime");
		String userId = req.getParameter("userId");
		String recipientId = req.getParameter("recipientId");
		String title = req.getParameter("title");
		if (StrUtil.isNotBlank(beginTime)) {
			queryWrapper.ge("begin_time", DateUtil.parse(beginTime, "yyyy-MM-dd HH:mm:ss"));
		}
		if (StrUtil.isNotBlank(endTime)) {
			queryWrapper.le("end_time", DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss"));
		}
		if (StrUtil.isNotBlank(userId)) {
			queryWrapper.eq("user_id", userId);
		}
		if (StrUtil.isNotBlank(recipientId)) {
			queryWrapper.like("recipient_id", recipientId);
		}
		if (StrUtil.isNotBlank(title)) {
			queryWrapper.like("title", title);
		}
		queryWrapper.orderByDesc("id");
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		// 只查询当前用户的信息
		queryWrapper.eq("create_by", user.getUsername());
		Page<RlTask> page = new Page<RlTask>(pageNo, pageSize);
		IPage<RlTask> pageList = rlTaskService.page(page, queryWrapper);
		List<RlTask> records = pageList.getRecords();
		if (CollUtil.isNotEmpty(records)) {
			records.forEach(e -> e.setFilePath(rlTaskService.convertFilePath(e.getFilePath())));
		}
		pageList.setRecords(records);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rlTask
	 * @return
	 */
	@AutoLog(value = "rl_task-添加")
	@ApiOperation(value="rl_task-添加", notes="rl_task-添加", hidden = true)
//	@RequiresPermissions("calendar:rl_task:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RlTask rlTask) {
		rlTaskService.save(rlTask);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param rlTask
	 * @return
	 */
	@AutoLog(value = "rl_task-编辑")
	@ApiOperation(value="rl_task-编辑", notes="rl_task-编辑", hidden = true)
//	@RequiresPermissions("calendar:rl_task:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RlTask rlTask) {
		rlTaskService.updateById(rlTask);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "rl_task-通过id删除")
	@ApiOperation(value="rl_task-通过id删除", notes="rl_task-通过id删除", hidden = true)
//	@RequiresPermissions("calendar:rl_task:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rlTaskService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "rl_task-批量删除")
	@ApiOperation(value="rl_task-批量删除", notes="rl_task-批量删除", hidden = true)
//	@RequiresPermissions("calendar:rl_task:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rlTaskService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "rl_task-通过id查询")
	@ApiOperation(value="rl_task-通过id查询", notes="rl_task-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RlTask> queryById(@RequestParam(name="id",required=true) String id) {
		RlTask rlTask = rlTaskService.getById(id);
		if(rlTask==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(rlTask);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rlTask
    */
//    @RequiresPermissions("calendar:rl_task:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RlTask rlTask) {
        return super.exportXls(request, rlTask, RlTask.class, "rl_task");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("calendar:rl_task:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RlTask.class);
    }

}
